﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NopSolutions.NopCommerce.Controls
{
    public class EmailTextBox : TextBox
    {
        private RequiredFieldValidator _rfv;
        private RegularExpressionValidator _rev;

        private string _errorCssClass = "error";
        private string _errorMessage = "*";
        private string _formatErrorMessage = "Email invalid.";
        private string _validationExpression = @".+@.+\..+";

        public EmailTextBox() {
            _rfv = new RequiredFieldValidator();
            _rev = new RegularExpressionValidator();
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            _rfv.ControlToValidate = this.ID;
            _rfv.CssClass = ErrorCssClass;
            _rfv.Display = ValidatorDisplay.Dynamic;
            _rfv.ErrorMessage = ErrorMessage;
            _rfv.ValidationGroup = ValidationGroup;
            Controls.Add(_rfv);

            _rev.ControlToValidate = this.ID;
            _rev.CssClass = ErrorCssClass;
            _rev.Display = ValidatorDisplay.Dynamic;
            _rev.ErrorMessage = FormatErrorMessage;
            _rev.ValidationExpression = ValidationExpression;
            _rev.ValidationGroup = ValidationGroup;
            Controls.Add(_rev);
        }

        protected override void Render(HtmlTextWriter w) {
            base.Render(w);
            _rfv.RenderControl(w);
            _rev.RenderControl(w);
        }

        public string ErrorMessage {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public string FormatErrorMessage {
            get { return _formatErrorMessage; }
            set { _formatErrorMessage = value; }
        }

        public string ErrorCssClass {
            get { return _errorCssClass; }
            set { _errorCssClass = value; }
        }

        public bool EnableClientScript {
            get { return _rfv.EnableClientScript; }
            set { _rfv.EnableClientScript = value; }
        }

        public string ValidationExpression {
            get { return _validationExpression; }
            set { _validationExpression = value; }
        }
    }
}
